#!/usr/bin/env python
import hashlib
import gobject
import gui.specialTextView

class Conversation:
   format_strong = False;
   format_italic = False;
   format_undrli = False;
   format_colour = False;
   format_hilite = False;
   format_big = False;
   format_small = False;

   sendEncryptKey = None;
   receiveEncryptKey = None;
   
   awaitingEncryptedMessages = {};
   
   _destroy = None; # Invoked when the conversation is destroyed

   private = True; # Is this a private conversation or a Multimx/Gallery?
   
   rememberKeyOnce = False;

   logRes = None;
   galleryMsgs = None;
   
   searchingAtIter = None;

   def __init__(self,buddy):
      self.buddy = buddy;
      self.convEntry = gui.specialTextView.specialTextView();
      self.sending_encrypted = False;
      self.logging_messages = True;

   @property
   def jid(self):
      return self.buddy.jid;

   def storeMessage(self,message):
      id = hashlib.md5(str(message.time)).hexdigest(); # Unique. MD5ed because
      self.awaitingEncryptedMessages[id] = message;
      return id;

   def enableEncryption(self,key):
      self.sendEncryptKey = key;
      self.sending_encrypted = True;
   
   def disableEncryption(self):
      self.sending_encrypted = False;

   def getFormat(self,i):
      return {
         0: self.format_strong,
         1: self.format_italic,
         2: self.format_undrli,
         3: self.format_hilite,
         4: self.format_big,
         5: self.format_small,
         6: self.format_colour,
      }[i];

   def destroy(self):
      if self._destroy is not None:
         self._destroy();

      self.convEntry.destroy();

      if hasattr(self,'view'):
         self.view.destroy();


class Conversations:
   lookup = [];
   conv = {};

   def __init__(self):
      pass;

   @property
   def len(self):
      return len(self.lookup);

   def __len__(self):
      return self.len;

   def addConversation(self,conversation):
      self.lookup.append(conversation.jid);
      self.conv[conversation.jid] = conversation;

   def updateConversation(self,jid,buddy):
      try:
         self.conv[jid].buddy = buddy;
         return self.conv[jid];
      except:
         return False;

   def removeConversationByJID(self,jid):
      try:
         self.conv[jid].destroy();
         self.lookup.pop(jid);
         del self.conv[jid];
      except:
         return None;

   def removeConversationByPageNumber(self,n):
      try:
         jid = self.lookup[n];
         self.conv[jid].destroy();
         self.lookup.pop(n)
         del self.conv[jid];
      except:
         return None;

   def getJIDFromPageNumber(self,n):
      try:
         return self.conv[self.lookup[n]].jid;
      except:
         return None;

   def getPageNumberFromJID(self,jid):
      try:
         return self.lookup.index(jid);
      except:
         return None;

   def getByJID(self,jid):
      try:
         return self.conv[jid];
      except:
         return None;

   def getByPageNumber(self,n):
      try:
         return self.conv[self.lookup[n]];
      except:
         return None;